package svgedit.japangui.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import svgedit.gui.PNGFileFilter;
import svgedit.japangui.JapanFrame;

@SuppressWarnings("serial")
public class SaveAsPNGAction extends AbstractAction{

	private JapanFrame frame;
	int width = 100;
    int height = 100;
    /** Create this action for the given frame */
    public SaveAsPNGAction(JapanFrame japanFrame) {
        super("\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8\u306F, PNG\u306B");
        this.frame = japanFrame;
    }

	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		File file = frame.getDocument().getFile();
        JFileChooser dialog;
        if (file != null)
            dialog = new JFileChooser(file);
        else
            dialog = new JFileChooser(frame.getPreferences().getDefaultPath());
        dialog.addChoosableFileFilter(new PNGFileFilter());
        dialog.showSaveDialog(null);

        file = dialog.getSelectedFile();
        if (file != null) {
            if (file.exists()) {
                if (JOptionPane.showConfirmDialog(frame.getRootPane(),
						String.format("\u3042\u306A\u305f\u304C%s\u3068\u3044\u3046\u540D\u524D\u306E\u65E2\u5b58\u306E\u30D5\u30A1\uFF72\uFF99\u3092\u7f6e\u304D\63db\u3048\u307e\u3059\u304B?",
								file.getName()), "\u4E0A\u66F8\u304D\u65E2\u5b58\u306E\u30D5\u30A1\uFF72\uFF99\u3092?",
                				JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION)
                    return;
            }
            frame.getPreferences().setDefaultPath(file.getPath());
            try {
				frame.savePNGFile(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		
	}
	}
}
